package action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import exception.DAOException;

import bo.BookManagementBO;
import bean.Book;

public class SearchBookAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){

		HttpSession session = request.getSession();
		BookManagementBO bookManagementBO = new BookManagementBO(); // Initial
																				// variable

		int page;
		try {
			page = request.getParameter("currentPage") != null ? Integer
					.parseInt(request.getParameter("currentPage")) : 1; // Get
																		// the
																		// current
																		// page
																		// index
		} catch (NumberFormatException e) {
			page = 1;
		}
		String choice = request.getParameter("choice") != null ? (request
				.getParameter("choice").toString()) : "";
		ArrayList<Book> bookList = new ArrayList<Book>();

		try {

			// Get keyword and searchBy from session
			String keyword = (String) session.getAttribute("book_keyword");
			String searchBy = (String) session.getAttribute("book_searchBy");
			int numberOfPage = bookManagementBO.getTotalBookPage(keyword, searchBy); // Get
			// total
			// number
			// of
			// pages

			/*
			 * If the chosen page is larger than total number of pages, set the
			 * page equal to total number of pages
			 */
			if (page > numberOfPage) {
				page = numberOfPage;
			}
			if (page < 1) {
				page = 1; // If the chosen page is less than 1, set it equal to
							// 1
			}

			/*
			 * If user choose "Precious", reset the list to chosen page and set
			 * current page attribute to chosen page
			 */
			if ("Previous".equals(choice)) {
				bookList = bookManagementBO.getAllBook(keyword, searchBy, page-1);
				page--;
			}

			/*
			 * If user choose "Next", reset the list to chosen page and set
			 * current page attribute to chosen page
			 */
			if ("Next".equals(choice)) {
				bookList = bookManagementBO.getAllBook(keyword, searchBy, page+1);
				page++;
			} else {
				/*
				 * If user choose "First" or "Last", reset the list to chosen
				 * page and set current page attribute to chosen page
				 */
				if ("First".equals(choice)) {
					page = 1;
				}
				if ("Last".equals(choice)) {
					page = numberOfPage;
				}
				bookList = bookManagementBO.getAllBook(keyword, searchBy, page);
			}

			/*
			 * If the index of page equals to total number of pages, set
			 * attribute isLastPage to true
			 */
			if (bookManagementBO.getTotalBookPage(keyword,
					searchBy) == page) {
				request.setAttribute("isLastPage", "true");
			}

			request.setAttribute("numberOfPage", bookManagementBO
					.getTotalBookPage(keyword, searchBy)); // Get
																			// total
			// number of
			// page
			request.setAttribute("currentPage", page);
			request.setAttribute("bookList", bookList);
		} catch (DAOException e) {
			return mapping.findForward("error");
		} catch (Exception e) {
			return mapping.findForward("error");
		}
		return mapping.findForward("bookTable");
	}
}
